<template>
  <div class="login-container">
    <div class="login-bgimg">
      <img src="../../assets/images/login.png" />
    </div>
    <div class="login-box">
      <div class="login-img"><img src="@/assets/images/login-logo.png" /></div>
      <div class="login-title">贵州电网信息中心IT运维平台</div>
      <el-form :model="loginForm" ref="loginForm" :rules="loginFormRules" @keyup.enter.native="login()">
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名/电话" prefix-icon="el-icon-s-custom" v-model="loginForm.username">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock" type="password">
          </el-input>
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="captcha">
              <el-input v-model="loginForm.captcha" placeholder="请输入验证码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item>
              <img style="width: 100%;" :src="loginForm.src" @click="refreshCaptcha">
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" class="login-button" @click="login">登 录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import { login, captcha } from '@/api/user'
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: '',
        captcha: '',
        src: ''
      },
      loginFormRules: {
        username: [{
          required: true,
          message: '请输入用户名/电话',
          trigger: 'blur'
        },
        {
          min: 3,
          max: 20,
          message: '长度在 3 到 20 个字符',
          trigger: 'blur'
        }
        ],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        },
        {
          min: 6,
          max: 18,
          message: '长度在 6 到 18 个字符',
          trigger: 'blur'
        }
        ],
        captcha: [{
          required: true,
          message: '请输入验证码',
          trigger: 'blur'
        }]
      }
    }
  },
  created () {
    this.queryCaptcha()
  },
  methods: {
    async loginIn (userInfo) {
      const data = await login(userInfo)
      if (data) {
        if (data.code === 200) {
          // 登录成功，跳转到主页
          Cookies.set('token', data.data.token)
          Cookies.set('userName', data.data.name)
          localStorage.setItem('themeName', 'theme-light')
          this.$router.push('/index')
        } else {
          this.queryCaptcha()
        }
      }
    },
    login () {
      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          let userInfo = {username: this.loginForm.username, password: this.loginForm.password, captcha: this.loginForm.captcha}
          this.loginIn(userInfo)
        } else {
          this.queryCaptcha()
          this.loginForm.captcha = ''
        }
      })
    },
    async queryCaptcha () {
      const data = await captcha()
      if (data.code === 200) {
        this.loginForm.src = 'data:text/css;base64,' + data.data.code
        Cookies.set('authorization', data.data.uuid)
      }
    },
    // 验证码
    refreshCaptcha: function () {
      Cookies.remove('authorization')
      this.queryCaptcha()
    }
  }
}
</script>

<style scoped>
  .login-container {
    width: 100%;
    height: 100%;
    background: url(../../assets/images/bg.jpg);
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
  }

  .login-box {
    width: 360px;
    height: 470px;
    background-color: rgba(255, 255, 255, 0.2);
    box-shadow: 0 0 10px #bebebe;
    border-radius: 12px;
    padding: 30px 20px 0 20px;
    position: absolute;
    left: 75%;
    top: 50%;
    transform: translate(-75%, -50%);
    /*设置上下左右居中 */
  }

  .login-img {
    width: 228px;
    height: 40px;
    margin: 0 auto;
  }

  .login-img img {
    width: 100%;
    height: 100%;
  }

  .login-title {
    color: #FFFFFF;
    font-size: 16px;
    line-height: 1.5em;
    margin-top: 10px;
    margin-bottom: 30px;
    text-align: center;
    letter-spacing: 4px
  }

  .el-form-item {
    margin-bottom: 35px;
  }

  .login-button {
    width: 100%;
  }

  .login-bgimg {
    width: 26.5rem;
    height: 33.3125rem;
    position: absolute;
    left: 25%;
    top: 50%;
    transform: translate(-25%, -50%);
    /*设置上下左右居中 */
  }

  .login-bgimg img {
    width: 100%;
    height: 100%;
  }
</style>
